﻿<%@ WebHandler Language="C#" Class="GetReportFile" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;

public class GetReportFile : IHttpHandler, IRequiresSessionState
{
    
    public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");

        //ConvertHtmlMimeToXml.convert(context.Server.MapPath("xmlHtmlMime.xml"), context.Server.MapPath("xmlMimeTypes.xml"));

        processRequest(context.Request,context.Response,context.Session,context.Server);

        //Dictionary<string, string> mimeTypes = ConvertHtmlMimeToXml.readData(context.Server.MapPath("xmlMimeTypes.xml"));
        
        
        
    }



    private void processRequest(HttpRequest request, HttpResponse response, HttpSessionState session,HttpServerUtility Server)
    {
        if (request["ID"] == null)
        {
            response.Write("Error , File not found!");
            response.Flush();
            response.Close();
            return;
            
        }

        String sessionID = request["ID"];

        if (session[sessionID] == null)
        {
            response.Write("Error , File not found!");
            response.Flush();
            response.Close();
            return;
            
        }

        byte[] btFile = new byte[0];
        try
        {
            btFile = (byte[])session[sessionID];
            

        }
        catch
        {
            response.Write("Error , File not found!");
            response.Flush();
            response.Close();
            return;
        }
        finally
        {
            session[sessionID] = null;
        }


        if (request["EXT"] == null)
        {
            response.Write("Error , File not found!");
            response.Flush();
            response.Close();
            return;

        }

        String ext = request["EXT"].ToLower();



        //attachment;
        response.AddHeader("Content-disposition","filename=Report." + ext);
        //response.AddHeader("filename", "Report." + ext);

        System.Collections.Generic.Dictionary<string, string> dr = ConvertHtmlMimeToXml.readData(Server.MapPath("xmlMimeTypes.xml"));
        if (dr[ext] != null)
            response.ContentType = dr[ext];
        

        response.BinaryWrite(btFile);

        response.Flush();
        response.Close();
        
        
        
        
        
    }
    
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}